{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Title: #设计机械累加器"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Difficulty: #Medium"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Category Title: #Algorithms"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Tag Slug: #bit-manipulation #recursion #brainteaser"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Name Translated: #位运算 #递归 #脑筋急转弯"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Solution Name: mechanicalAccumulator"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Translated Title: #设计机械累加器"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Translated Content:\n",
    "<p>请设计一个机械累加器，计算从 1、2... 一直累加到目标数值 <code>target</code> 的总和。注意这是一个只能进行加法操作的程序，不具备乘除、if-else、switch-case、for 循环、while 循环，及条件判断语句等高级功能。</p>\n",
    "\n",
    "<p>&nbsp;</p>\n",
    "\n",
    "<p><strong>示例 1：</strong></p>\n",
    "\n",
    "<pre>\n",
    "<strong>输入:</strong> target = 5\n",
    "<strong>输出:&nbsp;</strong>15\n",
    "</pre>\n",
    "\n",
    "<p><strong>示例 2：</strong></p>\n",
    "\n",
    "<pre>\n",
    "<strong>输入:</strong> target = 7\n",
    "<strong>输出:&nbsp;</strong>28\n",
    "</pre>\n",
    "\n",
    "<p>&nbsp;</p>\n",
    "\n",
    "<p><strong>提示：</strong></p>\n",
    "\n",
    "<ul>\n",
    "\t<li><code>1 &lt;= target&nbsp;&lt;= 10000</code></li>\n",
    "</ul>\n",
    "\n",
    "<p>&nbsp;</p>\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Description: [qiu-12n-lcof](https://leetcode.cn/problems/qiu-12n-lcof/description/)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Solutions: [qiu-12n-lcof](https://leetcode.cn/problems/qiu-12n-lcof/solutions/)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "test_cases = ['5', '7']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def mechanicalAccumulator(self, target: int) -> int:\n",
    "        return (1+target)*target//2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def mechanicalAccumulator(self, target: int) -> int:\n",
    "        res = 0\n",
    "        for i in range(0, target + 1):\n",
    "            res += i\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def mechanicalAccumulator(self, target: int) -> int:\n",
    "        return sum(range(target+1))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def mechanicalAccumulator(self, target: int) -> int:\n",
    "        return sum(range(1,target+1))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def mechanicalAccumulator(self, target: int) -> int:\n",
    "        return int((target + 1)/2 * target)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def mechanicalAccumulator(self, target: int) -> int:\n",
    "        return sum(list(range(1,target+1)))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def mechanicalAccumulator(self, target: int) -> int:\n",
    "        return sum([i for i in range(1,target + 1)])    "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def mechanicalAccumulator(self, target: int) -> int:\n",
    "        self.target = target\n",
    "        self.count = 0\n",
    "        def addcount():\n",
    "            if self.target == 0:return self.count\n",
    "            self.count+= self.target\n",
    "            self.target-=1\n",
    "            return addcount()\n",
    "        return addcount()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def mechanicalAccumulator(self, target: int) -> int:\n",
    "        \n",
    "        \n",
    "        def sumN(n):\n",
    "            return (n & 1 and not (n>>1)) or sumN(n - 1) + n\n",
    "\n",
    "        return sumN(target)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def mechanicalAccumulator(self, target: int) -> int:\n",
    "        \n",
    "        \n",
    "        def sumN(n):\n",
    "            if n == 1:\n",
    "                return 1\n",
    "            return sumN(n - 1) + n\n",
    "\n",
    "        return sumN(target)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def mechanicalAccumulator(self, target: int) -> int:\n",
    "        if target <1:return 0\n",
    "        else:\n",
    "            return self.mechanicalAccumulator(target-1)+target"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def mechanicalAccumulator(self, target: int) -> int:\n",
    "        if target <1:return 0\n",
    "        else:\n",
    "            return self.mechanicalAccumulator(target-1)+target"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def mechanicalAccumulator(self, target: int) -> int:\n",
    "        if target == 1:\n",
    "            return 1\n",
    "        else:\n",
    "            return self.mechanicalAccumulator(target-1) + target"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def mechanicalAccumulator(self, n: int) -> int:\n",
    "        result=0\n",
    "        def assist(n):\n",
    "            nonlocal result\n",
    "            n>1 and assist(n-1)\n",
    "            result+=n\n",
    "            return result\n",
    "        assist(n)\n",
    "        return result"
   ]
  }
 ],
 "metadata": {},
 "nbformat": 4,
 "nbformat_minor": 2
}
